import React from 'react';
import axios from 'axios';
import {Select } from 'antd';
const Option = Select.Option;

class omsSelect extends React.Component {
    constructor(props) {
        super(props);
        const value = props.initialValue || {};
        this.state = {
            currency: value.currency || '',
            options:value.options||[],
            id:"id",
            name:"name",
        };
    }


    componentDidMount() {
        if(this.props.url){
            if(this.props.property){
                this.setState({
                    id:this.props.property['id'],
                    name:this.props.property['name'],
                },()=>{console.log(this.state)})
            }
            this.getStuGroup();
        }
        if(this.props.data){
            this.setState(Object.assign(this.state.options, {options: this.props.data}))
        }
    }

    getStuGroup = (params) => {
        this.props.url().then(response => { 
            if (response&&response.data&&response.data.code==="200") {
                let res=response.data;
                if(this.props.transfromData){
                    res.data=this.props.transfromData(res.data)
                }
                this.setState(Object.assign(this.state.options, {options: res.data || []}))
            }
        })
        // axios.post(this.props.url, params).then(res => {
        //     if (res&&res.code==200) {
        //         this.setState(Object.assign(this.state.options, {options: res.data || []}))
        //     }
        // })
    }
    componentWillReceiveProps(nextProps) {
        // Should be a controlled component.
        if (!!nextProps.value) {
            const value = nextProps.value;
            this.setState(Object.assign(this.state.currency, {currency: value}))
        }
    }
    handleCurrencyChange = (currency) => {
        if (!('value' in this.props)) {
            this.setState({ currency });
        }
        this.triggerChange({ currency });
    }
    triggerChange = (changedValue) => {
        const onChange = this.props.onChange;
        if (onChange) {
            onChange(changedValue);
        }
    }


    render() {
        return (
                <Select
                    value={this.state.currency}
                    optionLabelProp="children" optionFilterProp="children"
                    onChange={this.handleCurrencyChange}
                    allowClear={true}
                    placeholder={this.props.placeholder||'请选择'}
                    {...this.props}
                >
                    {
                        this.state.options.map((item,index) =>
                            {   
                                return <Option value={item[this.state.id]} key={index}>{item[this.state.name]}</Option>
                            }
                        )
                    }
                </Select>
        )
    }
}

export default omsSelect;